package com.defysoft.waukids.intro;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseFragment;
import com.defysoft.waukids.R;
import com.google.android.gms.internal.el;

public class Intro_Status_F extends BaseFragment {
	View view;
	TextView tv;
	String sch;
	String stu;
	final int SUCCESS = 10;
	final int SUBSUCCESS = 9;
	final int FAIR = 8;
	final int STARTBUS = 7;

	public Intro_Status_F(String stu, String sch) {
		this.stu = stu;
		this.sch = sch;
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.activity_intro__status__f, null);
		setStartData();

		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		startNetwork(1);
	}

	@Override
	public void setStartData() {
		// TODO Auto-generated method stub
		super.setStartData();
		tv = (TextView) view.findViewById(R.id.INTRO_STATUS_TV);
	}

	@Override
	public void startThread(int choice) {
		// TODO Auto-generated method stub
		final int cho = choice;
		super.startThread(choice);
		firstThread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				switch (cho) {
				case 1:
					mainThread();
					break;
				}
			}
		});
		firstThread.start();
	}

	@Override
	protected void handlingMessage(Message _message) {
		// TODO Auto-generated method stub
		super.handlingMessage(_message);
		switch (_message.what) {
		case CATCH_EXCEPTION:
			if (firstThread == null) {
				firstThread.interrupt();
			}
			Toast.makeText(getActivity(), (String) _message.obj,
					Toast.LENGTH_LONG).show();
			break;
		case FAIR:
			if (firstThread == null) {
				firstThread.interrupt();
			}
			Toast.makeText(getActivity(), (String) _message.obj,
					Toast.LENGTH_SHORT).show();
			break;
		case SUCCESS:
			if (firstThread == null) {
				firstThread.interrupt();
			}
			tv.setText((String) _message.obj);
			break;
		}

	}

	public void mainThread() {
		JSONObject jsonObject = null;
		try {
			Log.i("tag","schcode=" + sch + "&stucode=" + stu);
			jsonObject = new JSONObject(publicHttpCallback(HTTP_STATUS_MASSEGE,
					"schcode=" + sch + "&stucode=" + stu));
			if (jsonObject.getBoolean("success")) {

				JSONArray ja = new JSONArray(jsonObject.getString("payload"));
				String msg = "";

				for (int i = 0; i < ja.length(); i++) {
					msg += (ja.getJSONObject(i).getString("Sta_Message") + "\n"
							+ ja.getJSONObject(i).getString("Sta_Date") + "\n\n");
				}
				onNetworkCallback(SUCCESS, msg);

			} else {
				if (jsonObject.getString("payload").equals("")) {
					onNetworkCallback(SUCCESS, "");
				}else {
					onNetworkCallback(FAIR, jsonObject.getString("message"));
				}
			}

		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString());
		}
	}
}
